package icraf.carbon.gui.model;

import static icraf.carbon.gui.MenuFactory.MenuProperty.MODEL_COSTB_TOTAL;
import static icraf.carbon.gui.MenuFactory.MenuProperty.MODEL_EMISSION;
import static icraf.carbon.gui.MenuFactory.MenuProperty.MODEL_GENERATE;
import static icraf.carbon.gui.MenuFactory.MenuProperty.MODEL_LANDUSE;
import static icraf.carbon.gui.MenuFactory.MenuProperty.MODEL_OPCOST;
import static icraf.carbon.gui.MenuFactory.MenuProperty.MODEL_SUMMARY;
import icraf.carbon.core.Config.ConfigItem;
import icraf.carbon.core.LandCover;
import icraf.carbon.core.LandCoverList;
import icraf.carbon.core.Project;
import icraf.carbon.core.ProjectIO;
import icraf.carbon.core.Zone;
import icraf.carbon.core.Zone.DataType;
import icraf.carbon.gui.LCMatrixTabPanel;
import icraf.carbon.gui.LCTableModel;
import icraf.carbon.gui.MatrixPanel;
import icraf.carbon.gui.MatrixTableModel;
import icraf.carbon.gui.MenuFactory.MenuProperty;
import icraf.carbon.gui.NumberRowHeader;
import icraf.carbon.gui.ProjectManager;
import icraf.carbon.gui.ProjectTreePanel;
import icraf.carbon.gui.RowHighlight;
import icraf.carbon.gui.StatusBar;
import icraf.carbon.model.ModelProject;
import icraf.common.gui.MenuAdapter;
import icraf.common.gui.MenuGenerator;
import icraf.common.gui.MenuGenerator.IconModel;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTable;
import javax.swing.ListModel;
import javax.swing.SwingWorker;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;

public class ModelPanel extends JPanel {



	public class Listener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource().equals(simButton)) {
				generateSimulation();
			} else if (MenuProperty.MODEL_CHECKOUT.isAction(e)) {
				checkoutProject();
			}
		}

	}

	class OptionPanel extends JPanel implements ActionListener, FocusListener {
		private static final long serialVersionUID = 8010365377254772411L;
		private MenuAdapter[] menu = { MenuProperty.MODEL_TPM,
				MenuProperty.MODEL_LANDUSE_SCENARIO };
		private MenuAdapter[] outputMenu = { MODEL_SUMMARY, MODEL_LANDUSE,
				MODEL_EMISSION, MODEL_COSTB_TOTAL, MODEL_OPCOST };
		
//		public JCheckBox projectBG = new JCheckBox();
//		public JCheckBox modifEmitCB = new JCheckBox();
		
		public OptionPanel() {
//			projectBG.addActionListener(this);
//			projectBG.addFocusListener(this);
//			JPanel p1 = new JPanel(new FlowLayout(FlowLayout.LEFT,5,0));
//			p1.add(new JLabel(MenuProperty.INPUT_INCLUDE_BG.getLabel()+":"));
//			p1.add(projectBG);
//			modifEmitCB.addActionListener(this);
//			modifEmitCB.addFocusListener(this);
//			JPanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT,5,0));
//			p2.add(new JLabel(MenuProperty.INPUT_INCLUDE_MODIF.getLabel()+":"));
//			p2.add(modifEmitCB);
			
			setLayout(new GridBagLayout());
			GridBagConstraints c = new GridBagConstraints();

			c.insets = new Insets(5, 5, 5, 5);
			c.fill = GridBagConstraints.BOTH;

			c.gridx = 0;
			c.gridy = 0;
			JLabel l = new JLabel(MenuProperty.MODEL_SCENARIO.getLabel()+ ":");
			l.setFont(getFont().deriveFont(Font.BOLD));
			add(l, c);

			for (MenuAdapter m : menu) {
				c.gridy++;
				add(MenuGenerator.createButton(m, this, true, IconModel.ICON16),
						c);
			}
//			c.gridy++;
//			add(p1, c);
//			c.gridy++;
//			add(p2, c);
			
			c.gridy++;
			JLabel l2 = new JLabel(MenuProperty.MODEL_OUTPUT.getLabel()+ ":");
			l2.setFont(getFont().deriveFont(Font.BOLD));
			add(l2, c);
			for (MenuAdapter m : outputMenu) {
				c.gridy++;
				add(MenuGenerator.createButton(m, this, true, IconModel.ICON16),
						c);
			}
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			for (int i = 0; i < menu.length; i++) {
				if (menu[i].isAction(e)) {
					projectTreePanel.setSelection(project, menu[i]);
					return;
				}
			}
			for (int i = 0; i < outputMenu.length; i++) {
				if (outputMenu[i].isAction(e)) {
					projectTreePanel.setSelection(project, outputMenu[i]);
					return;
				}
			}
//			if (e.getSource().equals(projectBG)) {
//				updateBG();
//			} else if (e.getSource().equals(modifEmitCB)) {
//				updateModif();
//			}
		}

//		private void updateBG() {
//			if(project == null) return;
//			ModelProject m = project.getModelProject();
//			if(m == null) return;
//			for(int i = 0; i <= m.getIterationCount(); i++) {
//				m.getSimulatedProject(i).setIncludeBelowgroundEmission(projectBG.isSelected());
//			}
//			m.updateSummary();
//			projectOutputModel.setModelProject(m);
//		}
//		
//		private void updateModif() {
//			if(project == null) return;
//			ModelProject m = project.getModelProject();
//			if(m == null) return;
//			for(int i = 0; i <= m.getIterationCount(); i++) {
//				m.getSimulatedProject(i).setIncludeModifiedEmission(modifEmitCB.isSelected());
//			}
//			m.updateSummary();
//			projectOutputModel.setModelProject(m);
//		}
		
		@Override
		public void focusGained(FocusEvent e) {
		}

		@Override
		public void focusLost(FocusEvent e) {
//			if (e.getSource().equals(projectBG)) {
//				updateBG();
//			} else if (e.getSource().equals(modifEmitCB)) {
//				updateModif();
//			}
		}
	}

	class ProjectOutputModel extends MatrixTableModel {
		private static final long serialVersionUID = 8968868974903218229L;
		private String header[] = { 
				MenuProperty.PROJECT.getLabel(),
				MenuProperty.MODEL_PERIOD.getLabel(), 
				MenuProperty.PROJECT_NET_EMISSION.getLabel() + " (" + MenuProperty.UNIT_EMISSION_HA.getLabel()+")" };
		private ModelProject modelProject;

		private NumberRowHeader tableListModel = new NumberRowHeader(this);

		@Override
		public Class<?> getColumnClass(int column) {
			return getValueAt(0, column).getClass();
		}

		@Override
		public int getColumnCount() {
			return header.length;
		}

		@Override
		public String getColumnName(int col) {
			return header[col];
		}


		public ModelProject getModelProject() {
			return modelProject;
		}

		@Override
		public int getRowCount() {
			return modelProject == null ? 0
					: modelProject.getIterationCount() + 1;
		}

		@SuppressWarnings({ "unchecked", "rawtypes" })
		@Override
		public ListModel getRowHeaderModel() {
			return tableListModel;
		}

		@Override
		public Object getValueAt(int row, int column) {
			if (modelProject == null)
				return null;
			Project p = modelProject.getSimulatedProject(row);
			switch (column) {
			case 0:
				return p.getLabel();
			case 1:
				return row;
			case 2:
				return p.getSummary().getEmissionPerHa() - p.getSummary().getSequestrationPerHa();
			}
			return null;
		}

		@Override
		public boolean isCellEditable(int row, int col) {
			return false;
		}

		public void setModelProject(ModelProject modelProject) {
			this.modelProject = modelProject;

			tableListModel.fireContentChange(getRowCount());
			fireTableDataChanged();
		}

	}

	private static final long serialVersionUID = 5251033989570690071L;

	private Listener listener = new Listener();
	private ModelScenarioPanel modelLUTTransPanel = new ModelScenarioPanel(this);
	private ModelScenarioPanel modelTPMPanel = new ModelScenarioPanel(this);
	private JLabel nLabel = new JLabel();
	private OptionPanel optionPanel = new OptionPanel();
	private Project project;
	private ProjectManager projectManager;
	private ProjectOutputModel projectOutputModel = new ProjectOutputModel();
	private MatrixPanel projectOutputTabel;
	private ProjectTreePanel projectTreePanel;
	private RowHighlight rowHighlightT;
	private RowHighlight rowHighlightTPM;
	private JButton simButton;
	private int updatedIndex = -1;
	private DataType updaterType;
	private CardLayout card;
	private JPanel mainPanel;
	private JProgressBar progressBar;
	private StatusBar statusBar;

	public ModelPanel(StatusBar statusBar) {
		super();
		this.statusBar = statusBar;
		JPanel buttonPanel = new JPanel(new GridLayout(2, 1));
		simButton = MenuGenerator.createButton(MODEL_GENERATE, listener, true,
				IconModel.ICON16);
		JPanel pb = new JPanel();
		pb.add(simButton);
		buttonPanel.add(pb);
		
		JPanel p = new JPanel();
		p.add(new JLabel(MenuProperty.MODEL_ITERASI.getLabel()+ ":"));
		p.add(nLabel);
		buttonPanel.add(p);
		
		JPanel tPanel = new JPanel(new BorderLayout());
		tPanel.add(createThreshPanel(), BorderLayout.NORTH);

		rowHighlightT = new RowHighlight() {
			@Override
			public Color getColor() {
				return new Color(250, 210, 200);
			}

			@Override
			public boolean isHighlight(MatrixTableModel table, int row, double f) {
				LCTableModel t = (LCTableModel)table;
				t.getValueType();
				int z = t.getZoneIndex();
				Zone zone = t.getProject().getZoneList().get(z);
				LandCoverList lcs = t.getProject().getLandCoverList();
				LandCover lc1 = lcs.get(row);
				double sum = 0;
				for(LandCover lc2: lcs) {
					double d = zone.getValue(lc1, lc2, DataType.LANDUSE_SCENARIO_B, null);
					d = t.isAreaScale()?d*zone.getAreaSize():d;
					sum += Double.isNaN(d)?0:d;
				}
				double precision = t.isAreaScale()?ConfigItem.PRECISION_MIN.getValueDouble():ConfigItem.PRECISION_MAX.getValueDouble();
				double precision10 = Math.pow(10, precision);
				sum = Math.round(sum * precision10) / precision10;
				f = Math.round( f * precision10) / precision10;
				//System.out.println("z="+zone.getLabel()+" f="+f+" sum="+sum +" r="+ row + " m="+t.getValueAt(row, t.getColumnCount()-1));

				return f != sum;
			}
		};
		
		rowHighlightTPM = new RowHighlight() {
			@Override
			public Color getColor() {
				return new Color(250, 210, 200);
			}

			@Override
			public boolean isHighlight(MatrixTableModel table, int row, double f) {
				double precision = ConfigItem.PRECISION_MED.getValueDouble();
				double precision10 = (float)Math.pow(10, precision);
				f = Math.round(f * precision10) / precision10;
				return f != 1;
			}
		};
		modelTPMPanel.setRowHighlight(true, rowHighlightTPM);
		modelLUTTransPanel.setRowHighlight(true, rowHighlightT);
		modelLUTTransPanel.setEnableMenu(false);
		

		projectOutputTabel = new MatrixPanel(projectOutputModel);
		projectOutputTabel.getTable().setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
		projectOutputTabel.getPopMenu().addSeparator();
		projectOutputTabel.getPopMenu().add(
				MenuGenerator.createMenuItem(MenuProperty.MODEL_CHECKOUT,
						listener));
		projectOutputTabel.setBorder(BorderFactory.createEmptyBorder(20, 10, 20, 10));
		this.setLayout(new BorderLayout());
		this.add(buttonPanel, BorderLayout.NORTH);
		
		JPanel resultP = new JPanel(new BorderLayout());
		resultP.add(projectOutputTabel, BorderLayout.CENTER);
		resultP.add(optionPanel, BorderLayout.EAST);
		
		
		progressBar = new JProgressBar(0, progressMax);
		progressLabel = new JLabel(MenuProperty.MODEL_WAIT.getLabel());
		progressLabel.setAlignmentX(JLabel.CENTER_ALIGNMENT);
		//progressBar.setIndeterminate(true);
		JPanel progressP = new JPanel();
		progressP.setBorder(BorderFactory.createEmptyBorder(50, 50, 50, 50));
		progressP.setLayout(new BoxLayout(progressP, BoxLayout.PAGE_AXIS));
		progressP.add(Box.createVerticalGlue());
		progressP.add(progressLabel);
		progressP.add(progressBar);
		progressP.add(Box.createVerticalGlue());
		
		
		card = new CardLayout();
		
		mainPanel = new JPanel(card);
		mainPanel.add(new JPanel(), DEFAULT);
		mainPanel.add(resultP, OUTPUT);
		mainPanel.add(progressP, PROGRESS);
		this.add(mainPanel);
	}

	private static String OUTPUT = "output";
	private static String PROGRESS = "progress";
	private static String DEFAULT = "default";
	
	public void checkoutProject() {
		JTable table = projectOutputTabel.getTable();
		if (table.getSelectedRowCount() <= 0)
			return;
		int opt = JOptionPane
				.showConfirmDialog(
						this,
						MenuProperty.MODEL_CHECKOUT_MSG.getAccessibleDescription(),
						MenuProperty.MODEL_CHECKOUT_MSG.getLabel(),
						JOptionPane.YES_NO_OPTION);
				if (opt != JOptionPane.YES_OPTION)
					return;
		int[] rowsselected = table.getSelectedRows();
		for (int r : rowsselected) {
			if (r == 0)
				continue;
			Project p = project.getModelProject().getSimulatedProject(r);
			if (projectManager != null)
				projectManager.add(ProjectIO.duplicateProject(p));
		}
	}


	
	protected boolean checkLUS() {
//		double precision = ConfigItem.PRECISION_MAX.getValueDouble();
//		double precision10 = (float)Math.pow(10, precision);
//		ModelProject m = project.getModelProject();
//		for(int i = 0; i < m.getIterationCount()-1; i++) {
//			Project p = m.getSimulatedProject(i);
//			for(int z = 0; z < project.getNumberOfZone(); z++) {
//				Zone zone = p.getZoneList().get(z);
//				double v = zone.getValueTotal(DataType.LANDUSE_SCENARIO, null);
//				v = Math.round(v * precision10) / precision10;
//				if ((float) v != 1) {
//					return false;
//				}
//			}
//		}
//		return true;
//double precision = ConfigItem.PRECISION_MED.getValueDouble();
		
		// check for validation
		ModelScenarioPanel msp = modelLUTTransPanel;
		for (int i = 0; i < msp.getTabPanel().getTabCount(); i++) {
			LCMatrixTabPanel mtp = (LCMatrixTabPanel) msp.getTabPanel()
					.getComponentAt(i);
			for (int j = 0; j < mtp.getTabPanel().getTabCount(); j++) {
				MatrixPanel mp = (MatrixPanel) mtp.getTabPanel()
						.getComponentAt(j);
				MatrixTableModel mtm = mp.getMatrixTableModel();
				
				LCTableModel t = (LCTableModel)mtm;
				t.getValueType();
				int z = t.getZoneIndex();
				Zone zone = t.getProject().getZoneList().get(z);
				LandCoverList lcs = t.getProject().getLandCoverList();
				
				double precision = t.isAreaScale()?ConfigItem.PRECISION_MIN.getValueDouble():ConfigItem.PRECISION_MAX.getValueDouble();
				double precision10 = Math.pow(10, precision);
				double x = mp.isTotalFields()? 1:0;
				
				for (int r = 0; r < mtm.getRowCount() - x; r++) {
				
					LandCover lc1 = lcs.get(r);
					double sum = 0;
					for(LandCover lc2: lcs) {
						double d = zone.getValue(lc1, lc2, DataType.LANDUSE_SCENARIO_B, null);
						d = t.isAreaScale()?d*zone.getAreaSize():d;
						sum += Double.isNaN(d)?0:d;
					}
					sum = Math.round(sum * precision10) / precision10;
					
					double v = 0;
					for (int c = 0; c < mtm.getColumnCount() - x; c++) {
						v += (Double) mtm.getValueAt(r, c);
					}
					v = Math.round(v * precision10) / precision10;
					
					if ((v != sum) && (v != 0)) {
						msp.getTabPanel().setSelectedIndex(i);
						mtp.getTabPanel().setSelectedIndex(j);
						return false;
					}
				}
			}
		}
		return true;
		
	}

	
	
	protected boolean checkTPM() {
		double precision = ConfigItem.PRECISION_MED.getValueDouble();
		
		// check for validation
		ModelScenarioPanel msp = modelTPMPanel;
		for (int i = 0; i < msp.getTabPanel().getTabCount(); i++) {
			LCMatrixTabPanel mtp = (LCMatrixTabPanel) msp.getTabPanel()
					.getComponentAt(i);
			for (int j = 0; j < mtp.getTabPanel().getTabCount(); j++) {
				MatrixPanel mp = (MatrixPanel) mtp.getTabPanel()
						.getComponentAt(j);
				MatrixTableModel mtm = mp.getMatrixTableModel();
				double v = 0;
				double x = 0;
				if (mp.isTotalFields()) {
					x = 1;
				}
				double precision10 = Math.pow(10, precision);
				for (int r = 0; r < mtm.getRowCount() - x; r++) {
					v = 0;
					for (int c = 0; c < mtm.getColumnCount() - x; c++) {
						v += (Double) mtm.getValueAt(r, c);
					}
					v = Math.round(v * precision10) / precision10;
					if ((v != 1) && (v != 0)) {
						msp.getTabPanel().setSelectedIndex(i);
						mtp.getTabPanel().setSelectedIndex(j);
						return false;
					}
				}
			}
		}
		return true;
	}

	private JPanel createThreshPanel() {
		JPanel thresP = new JPanel();
		JLabel label = new JLabel(
				MenuProperty.PROJECT_SUMMARY_THRESHOLD.getLabel() + ":");
		JButton bUpdate = MenuGenerator.createButton(MenuProperty.EDIT_UPDATE,
				listener, true, IconModel.ICON16);
		thresP.add(label);
		thresP.add(bUpdate);
		return thresP;
	}

	public void generateSimulation() {
		if (project.getModelProject() != null) {
			int opt = JOptionPane
					.showConfirmDialog(
							this,
							MenuProperty.MODEL_UPDATE_MSG.getAccessibleDescription(),
							MenuProperty.MODEL_UPDATE_MSG.getLabel(),
							JOptionPane.YES_NO_OPTION);
			if (opt != JOptionPane.YES_OPTION)
				return;
		}
		int n = 0;
		while (n <= 0) {
			int defN = 1;
			if (project.getModelProject() != null) {
				defN = project.getModelProject().getIterationCount();
			}
			String inp = (String) JOptionPane.showInputDialog(this,
					MODEL_GENERATE.getAccessibleDescription() + ":\r\n\""
							+ project.getLabel() + "\"\r\n"
							+ MenuProperty.MODEL_ITERASI.getLabel()
							+ ":",
					MODEL_GENERATE.getLabel(), JOptionPane.PLAIN_MESSAGE, null,
					null, defN);
			if (inp == null)
				return;
			try {
				n = Integer.parseInt(inp);
			} catch (NumberFormatException excp) {
				n = -1;
			}
		}

		card.show(mainPanel, PROGRESS);
		Simulator simulator = new Simulator(n);
		
		simulator.execute();
		progressUpdate = 0;
		Task task = new Task();
		task.addPropertyChangeListener(
			     new PropertyChangeListener() {
			         public  void propertyChange(PropertyChangeEvent evt) {
			             if ("progress".equals(evt.getPropertyName())) {
			                 progressBar.setValue((Integer)evt.getNewValue());
			             }
			         }
			     });
		task.execute();
	}

	class Simulator extends SwingWorker<Void, Void> {
		private int nIteration;
		
		Simulator(int n) {
			nIteration = n;
		}

		@Override
		public Void doInBackground() {
			project.generateSimulationModel(nIteration);
			updateOutput(project.getModelProject());
			return null;
		}

		 @Override
       protected void done() {
//			 updateOutput(project.getModelProject());
       }

		
	}
	
	class Task extends SwingWorker<Void, Void> {
        /*
         * Main task. Executed in background thread.
         */
        @Override
        public Void doInBackground() {
           int progress = 0;
            //Initialize progress property.
            setProgress(0);
            
            while (progress < progressMax) {
                try {
                    Thread.sleep(200);
                } catch (InterruptedException ignore) {}
                //Make random progress.
                double d = project.getSimulationProgress();
                if(d < 0.9) {
                	progressLabel.setText(MenuProperty.MODEL_RUN.getLabel());
                	progress = (int)(progressMax*d*0.5);
                } else {
                	progressLabel.setText(MenuProperty.MODEL_PROCESS.getLabel());
                	progress = (int)(progressMax*(progressUpdate*0.5 + 0.5));
               }
                setProgress(Math.min(progress, progressMax));
            }
            return null;
        }

        /*
         * Executed in event dispatch thread
         */
        public void done() {
            
        }
    }
	
	private int progressMax = 100;
	
	public ModelScenarioPanel getModelTPMPanel() {
		return modelTPMPanel;
	}

	public ModelScenarioPanel getModelTransitionPanel() {
		return modelLUTTransPanel;
	}

	public Project getProject() {
		return project;
	}

	public int getUpdatedIndex() {
		return updatedIndex;
	}

	public void setProject(Project project, ProjectManager projectManager) {
		this.project = project;
		if (project.getModelProject() == null) {
			simButton.setText(MenuProperty.MODEL_GENERATE.getLabel());
			nLabel.setText("");
		}
		updateOutput(project.getModelProject());
		modelLUTTransPanel.setZoneAreaScale(true);
	}

	public void setProjectManager(ProjectManager projectManager) {
		this.projectManager = projectManager;
	}

	public void setProjectTreePanel(ProjectTreePanel p) {
		this.projectTreePanel = p;
	}

	public void updateModelScenario(int period, DataType type) {
		int idx = updatedIndex;

		int opt = JOptionPane.showConfirmDialog(this,
				MenuProperty.MODEL_SCENARIO_UPDATE.getAccessibleDescription() + " ("+MenuProperty.MODEL_ACTIVE.getLabel()+": " +period+" )",
				MenuProperty.MODEL_SCENARIO_UPDATE.getLabel(),
				JOptionPane.YES_NO_OPTION);
		if (opt != JOptionPane.YES_OPTION)
			return;

		if (type == DataType.LANDUSE_SCENARIO) {
			if (!checkLUS()) {
				JOptionPane
						.showMessageDialog(
								this,
								MenuProperty.MODEL_INVALID_VALUE.getAccessibleDescription(),
								MenuProperty.MODEL_INVALID_VALUE.getLabel(),
								JOptionPane.ERROR_MESSAGE);
				return;
			}
		} else if (type == DataType.TPM_SCENARIO) {
			if (!checkTPM()) {
				JOptionPane
						.showMessageDialog(
								this,
								MenuProperty.MODEL_INVALID_ROW.getAccessibleDescription(),
								MenuProperty.MODEL_INVALID_ROW.getLabel(),
								JOptionPane.ERROR_MESSAGE);
				return;
			}
		}

		ModelProject m = project.getModelProject();
		Project p = m.getSimulatedProject(idx);
		m.runSimulationModel(idx, p);
		updateOutput(m);

		updatedIndex = -1;
		modelTPMPanel.resetLabel();
		modelLUTTransPanel.resetLabel();
	}

	private double progressUpdate =0;

	private JLabel progressLabel;
	
	public void updateOutput(ModelProject mp) {
		progressUpdate = 0;
		projectOutputModel.setModelProject(mp);
		progressUpdate = 0.2;
		modelTPMPanel.setModelProject(mp,
				DataType.TPM_SCENARIO, null);
		progressUpdate = 0.5;
		modelLUTTransPanel.setModelProject(mp,
				DataType.LANDUSE_SCENARIO, null);
		progressUpdate = 0.8;
		modelTPMPanel.setCornerSetting(MenuProperty.UNIT_PROBABILITY.getLabel(), "", false);
		modelLUTTransPanel
				.setCornerSetting(
						MenuProperty.UNIT_FRAC_HA.getLabel(),
						MenuProperty.UNIT_HECTARE.getLabel(), true);
		modelLUTTransPanel.setZoneAreaScale(true);
		progressUpdate = 1;

		if (mp != null) {
			nLabel.setText(Integer.toString(mp.getIterationCount()));
			simButton.setText(MenuProperty.MODEL_REGENERATE.getLabel());
			card.show(mainPanel, OUTPUT);
		} else {
			card.show(mainPanel, DEFAULT);
		}
//		optionPanel.projectBG.setSelected(project.isIncludeBelowgroundEmission());
//		optionPanel.modifEmitCB.setSelected(project.isIncludeModifiedEmission());
	}

	public void updateScenario(int updatedIndex, DataType updater) {
		this.updatedIndex = updatedIndex;
		if(updatedIndex >= 0) {
			modelLUTTransPanel.setUpdateEnable(true);
			modelTPMPanel.setUpdateEnable(true);
		}
		this.updaterType = updater;
	}

	public DataType getUpdaterType() {
		return updaterType;
	}


}
